class Solution:
    def divide(self, dividend: int, divisor: int) -> int:
        flag = 1 if (dividend > 0 and divisor > 0) or (dividend < 0 and divisor < 0) else -1

        dividend = abs(dividend)
        divisor = abs(divisor)
        res = 0
        while dividend >= divisor:
            tmp = divisor
            i = 1
            while dividend >= tmp:
                dividend -= tmp
                res+=i
                i<<=1
                tmp<<=1
        res = flag * res
        return min(max(-2**31, res), 2**31-1)